/*
 *  * Copyright (C) 2021 Huawei Device Co., Ltd.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.jaredrummler.materialspinner;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import ohos.global.configuration.Configuration;
import ohos.multimodalinput.event.KeyEvent;

import java.util.ArrayList;
import java.util.Collections;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class PopupCustomDialog extends CommonDialog implements Component.ClickedListener, MaterialSpinnerBaseAdapter.InternalListCallback {
    private static final String TAG = PopupCustomDialog.class.getSimpleName();
    private ListContainer listContainer;
    final Builder builder;

    private PopupCustomDialog(Builder builder) {
        super(builder.getContext());
        this.builder = builder;
        ComponentContainer rootLayout = (ComponentContainer) LayoutScatter.getInstance(builder.getContext()).parse(getInflateLayout(builder), null, false);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setShape(ShapeElement.RECTANGLE);
        shapeElement.setRgbColor(new RgbColor(((builder.backgroundColor >> 16) & 0xFF),
                ((builder.backgroundColor >> 8) & 0xFF),
                ((builder.backgroundColor) & 0xFF)));
        Component boxroot = rootLayout.findComponentById(ResourceTable.Id_box_root);
        boxroot.setBackground(shapeElement);

        prepareDialogView(rootLayout, builder);
        setContentCustomComponent(rootLayout);
    }

    private void prepareDialogView(ComponentContainer rootLayout, Builder builder) {
        listContainer = UiTUtil.getComponent(rootLayout, ResourceTable.Id_listContainer);

        if (listContainer != null && builder.baseItemProvider == null) {
            builder.baseItemProvider = new MaterialSpinnerAdapterWrapper(builder.getContext(), this, builder.backgroundSelecorElement, builder.textColor, builder);
        }
        listContainer.enableScrollBar(Component.VERTICAL,true);
        listContainer.setScrollbarColor(Color.DKGRAY);
        invalidateList();
    }

    @Override
    public void onClick(Component component) {
        hide();
    }

    @Override
    public boolean clickKeyDown(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEY_BACK) {
            hide();
            builder.nothingListCallback.onSelection(this, null, 0, "");
        }
        return super.clickKeyDown(event);
    }

    @Override
    public boolean onItemSelected(PopupCustomDialog dialog, Component itemView, int position, CharSequence text, boolean longPress) {
        if (!longPress && builder.listCallback != null) {
            builder.listCallback.onSelection(this, itemView, position, builder.items.get(position));
            destroy();
        }
        return true;
    }

    public static class Builder {
        private Context context;
        ArrayList<CharSequence> items;
        BaseItemProvider baseItemProvider;
        ListCallback listCallback;
        NothingListCallback nothingListCallback;
        private CommonDialog dialog;
        int backgroundColor;
        int backgroundSelecorElement;
        Color textColor;
        int popupPaddingLeft;
        int popupPaddingTop;
        int popupPaddingRight;
        int popupPaddingBottom;
        int popupMaxHeight;


        public Builder(Context context) {
            this.context = context;
        }

        public final Context getContext() {
            return context;
        }

        public Builder setBackgroundColor(int color){
            this.backgroundColor = color;
            return this;
        }

        public Builder setBackgroundSelecorColor(int backgroundSelecorElement){
            this.backgroundSelecorElement = backgroundSelecorElement;
            return this;
        }

        public Builder setCustomTextColor(Color textColor){
            this.textColor = textColor;
            return this;
        }

        public Builder setPopupPadding(int left, int top, int right, int bottom){
            this.popupPaddingLeft = left;
            this.popupPaddingTop = top;
            this.popupPaddingRight = right;
            this.popupPaddingBottom = bottom;
            return this;
        }

        public Builder setMaxPopupHeight(int height){
            this.popupMaxHeight = height;
            return this;
        }

        public Builder items(CharSequence... items) {
            this.items = new ArrayList<>();
            Collections.addAll(this.items, items);
            return this;
        }

        public Builder itemsCallback(ListCallback callback) {
            this.listCallback = callback;
            return this;
        }

        public Builder nothingItemsCallback(NothingListCallback callback) {
            this.nothingListCallback = callback;
            return this;
        }


        public void show() {
            int orientation = context.getResourceManager().getConfiguration().direction;
            if (orientation == Configuration.DIRECTION_VERTICAL) {
                new PopupCustomDialog(this).setAlignment(LayoutAlignment.TOP).setOffset(0, 450).setSize(980, popupMaxHeight).show();
            } else {
                new PopupCustomDialog(this).setAlignment(LayoutAlignment.TOP).setOffset(0, 450).setSize(2100, popupMaxHeight).show();
            }
        }

        public void dismiss() {

        }
    }

    public interface ListCallback {
        void onSelection(PopupCustomDialog dialog, Component itemView, int position, CharSequence text);
    }

    public interface NothingListCallback {
        void onSelection(PopupCustomDialog dialog, Component itemView, int position, CharSequence text);
    }

    private int getInflateLayout(Builder builder) {
        return ResourceTable.Layout_uilayouts_stack_layout;
    }

    private void invalidateList() {
        if (listContainer == null) {
            return;
        } else if ((builder.items == null || builder.items.size() == 0) && builder.baseItemProvider == null) {
            return;
        }
        listContainer.setItemProvider(builder.baseItemProvider);
        ((MaterialSpinnerAdapterWrapper) builder.baseItemProvider).setCallback(this);
    }
}
